const lbbTaskService = require('../service/task-service.js');

let calendar;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    startDate: null,
    showLevel: false,
    levels: [
      { level: 1, text: '10台及以下' },
      { level: 2, text: '11-20台' },
      { level: 3, text: '21-30台' },
      { level: 4, text: '31-40台' },
      { level: 5, text: '41-50台' },
      { level: 6, text: '51-60台' },
      { level: 7, text: '61-70台' },
      { level: 8, text: '71及以上' },
    ],
    selLevelIndex: null,
    defLevelIndex: 0,
    refreshedPickup: true,
    stepDay: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getTaskPhotoTimeConfById();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    calendar = this.selectComponent("#calendar")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onDisplay() {
    calendar.show()
  },
  onChange(event) {
    console.log(event.detail);
    this.data.stepDay = event.detail
  },
  change(e) {
    //选择结果
    console.log(e);
    this.setData({
      startDate: e.detail.result,
    })
  },
  showLevelPicker() {
    this.setData({
      refreshedPickup: false,
      defLevelIndex: this.data.selLevelIndex != null ? this.data.selLevelIndex : 0,
    })
    this.setData({
      refreshedPickup: true,
      showLevel: true,
    })
  },
  onLevelConfirm(event) {
    this.setData({
      selLevelIndex: event.detail.index,
      showLevel: false,
    })
  },
  onLevelClose() {
    this.setData({
      showLevel: false,
    })
  },
  onLevelCancel() {
    this.setData({
      showLevel: false,
    })
  },

  getTaskPhotoTimeConfById() {
    let that = this
    lbbTaskService.getTaskTimeConfById({
      id:0
    },
    function(res) {
      let data = res.data
      that.setData({
        levels: data.level,  
        selLevelIndex: data.selLevelIndex,
      })
    })
  },

  /**
   * 保存设置
   */
  saveConf(){
    let that = this

    lbbTaskService.setTaskTimeConf({
      startDateStr: that.data.startDate, 
      stepDay: that.data.stepDay,
      selLevelIndex: that.data.selLevelIndex
    }, function (res){

     wx.showToast({
       title: res.result == 0 ? '更新完成' : res.data.description,
     })

    })
  },

})